﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Splittergame
{
    class SplitterParticle
    {
        public int m_seed;
        private Vector2 SplitterPosition;
        private Vector2 velocity;
        private Vector2 acceleration;
        private Texture2D ParticleTexture;
        private float Size;
        float killSpeed;
        float particleLifeTime = 3f;


        public SplitterParticle(int seed, Texture2D texture, Vector2 startPosition) 
        {
            m_seed = seed;         
            acceleration = new Vector2(0, 0.08f);  
            Random rand = new Random(m_seed);
            ParticleTexture = texture;
            SplitterPosition = startPosition;

            velocity = new Vector2((float)(rand.NextDouble() * 1.2 - 0.8), (float)(rand.NextDouble() * 1.2 - 0.8));
            velocity.Normalize();           

            // hastighetgenerering

            float min = 2.5f;
            float max = 3.5f;
            float particleSpeed = min + ((float)(rand.NextDouble()) * (max-min));

            acceleration *= particleSpeed;

            // Storleksgenerering

            float minSize = 0.02f;
            float maxSize = 1.0f;

            Size = minSize + ((float)(rand.NextDouble()) * (maxSize - minSize));

            // livslängd

            killSpeed = 0.02f;

        }

        internal void Update(GameTime elapsedTimeSeconds) 
        {
            Random rand = new Random(m_seed);

            velocity = velocity + acceleration * elapsedTimeSeconds.ElapsedGameTime.Milliseconds/100;
            SplitterPosition = SplitterPosition + velocity * elapsedTimeSeconds.ElapsedGameTime.Milliseconds/500;

            particleLifeTime = particleLifeTime - killSpeed;

            if (particleLifeTime <= 0)
            {
                //SplitterPosition.X = 5;
                //SplitterPosition.Y = 5;

                particleLifeTime = 3;

                velocity = new Vector2((float)(rand.NextDouble() * 1.5 - 1.0), (float)(rand.NextDouble() * 2.0 - 1.0));
                velocity.Normalize();   
            }
        }

        internal void Draw(SpriteBatch spriteBatch, Camera camera)
        {

            Rectangle ballViewRect = camera.getVisualRect(SplitterPosition.X, SplitterPosition.Y, 0.2f);
            spriteBatch.Draw(ParticleTexture, ballViewRect, Color.White);
        
        }

    }
}
